package mysql

import (
	"database/sql"
	"fmt"
	"os"
	"easycharts/utils"


	_ "github.com/go-sql-driver/mysql"
)

var db *sql.DB

func InitDb() {
	// db, _ = sql.Open("mysql", "ring:0229ljl@tcp(127.0.0.1:3306)/bmstest?charset=utf8")
	// 根据配置文件连接数据库
	db, _ = sql.Open(utils.Db, fmt.Sprintf("%s:%s@(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local",
		utils.DbUser,
		utils.DbPassWord,
		utils.DbHost,
		utils.DbPort,
		utils.DbName,
	))

	db.SetMaxOpenConns(1000)
	err := db.Ping()
	if err != nil {
		fmt.Println("Failed to connect to mysql, err" + err.Error())
		os.Exit(1)
	}
}

func DBConn() *sql.DB {
	return db
}

